//
//  HomeMenuView.m
//  CarOfHome2
//
//  Created by dlios on 15/9/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeMenuView.h"

#import "STYHomeBtnModel.h"

@interface HomeMenuView ()



@property (nonatomic, strong) NSMutableArray *Arr;

@end

@implementation HomeMenuView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _butArr = [NSMutableArray array];
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 345 * SAWindth, 30 * SAHeight)];
        [self addSubview:self.scrollView];


        self.addMenu = [UIButton buttonWithType:UIButtonTypeCustom];
        self.addMenu.frame = CGRectMake(345 * SAWindth, 0 * SAHeight, 30 * SAWindth, 30 * SAHeight);
        [self.addMenu setImage:[UIImage imageNamed:@"addButtonPic.png"] forState:UIControlStateNormal];
        [self.addMenu setImage:[UIImage imageNamed:@"shangjiantou.png"] forState:UIControlStateSelected];
        [self addSubview:self.addMenu];


    }
    return self;
}

- (void)setButArr:(NSMutableArray *)butArr
{
    if (_butArr != butArr) {
        _butArr = butArr;
    }
    

//    NSArray *tempBtuArr = self.scrollView.subviews;
//
//    NSLog(@"%@",tempBtuArr);
    
    
    for (__strong UIButton *button in self.scrollView.subviews) {
        [button removeFromSuperview];
        button = nil;
    }
    
    
    
    NSInteger btnCount = butArr.count;
    
    
    self.Arr = [NSMutableArray array];
    
    NSInteger btnLocationX = 12.5;
    for (int i = 0; i < btnCount; i++ ) {
        
        STYHomeBtnModel *btnModel = [self.butArr objectAtIndex:i];
        
       UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSInteger length = btnModel.btnName.length * 18 + 14;
       
        but.frame = CGRectMake(btnLocationX * SAWindth, 0, length * SAWindth, 30 * SAHeight);
         btnLocationX += length;
        but.tag = 100 + btnModel.btnIndex;
        [but setTitle:btnModel.btnName forState:UIControlStateNormal];
        [but setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        but.titleLabel.font = [UIFont systemFontOfSize:15 * SAHeight];
        [but addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.Arr addObject:but];
        
        [self.scrollView addSubview:but];
        
    }
    
    self.scrollView.contentSize = CGSizeMake(btnLocationX * SAWindth , 30 * SAHeight);
  
}

- (void)setIndex:(NSInteger)index
{
    if (_index != index) {
        _index = index;
    }

    UIButton *button = [self.Arr objectAtIndex:index];
    
    button.selected = YES;
    
    
}

- (void)butClicked:(UIButton *)button
{
    [self changeState];
    button.selected = YES;
    STYHomeBtnModel *model = [[STYHomeBtnModel alloc] init];
    
    model.btnName = button.titleLabel.text;
    model.btnIndex = button.tag - 100;
    
    [self.delegate passValueWithBtnModel:model];
    
}

- (void)changeState
{
    for (UIButton *but in self.Arr) {

        but.selected = NO;

    }
    
}

@end
